/* eslint valid-jsdoc: "off" */
const {DB} = require('./db.config')
"use strict";

/**
 * @param {Egg.EggAppInfo} appInfo app info
 */
module.exports = (appInfo) => {
  /**
   * built-in config
   * @type {Egg.EggAppConfig}
   **/
  const config = (exports = {});

  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + "_1558157321746_5590";

// 加载 errorHandler 中间件
    config.middleware = ['errorHandler', 'jwtHandler', 'jwt', 'authz'];
// 只对 /api 前缀的 url 路径生效
    config.errorHandler = {
        // match: '/api',
    };
// jwt配置
    config.jwt = {
        secret: 'exe-tools',
        expiresIn: "8h",
        ignore(ctx) {
            // todo 目前先排除正在自动化使用到的接口
            // const reg = /\/api\/v1\/colors|\/autopack/g;
            // // console.log(ctx.request.url);
            // // console.log(reg.test(ctx.request.url));
            // if(ctx.request.url === '/') {
            //     return true
            // }
            // if(reg.test(ctx.request.url)) {
            //     return true
            // }
            // return reg.test(ctx.request.url);
        },
        getToken: function fromHeaderOrQuerystring(ctx) {
            if (ctx.headers.authorization && ctx.headers.authorization.split(" ")[0] === "Bearer") {
                return ctx.headers.authorization.split(" ")[1];
            } else if (ctx.query && ctx.query.token) {
                return ctx.query.token;
            } else if (ctx.cookies.get('token')) {
                return ctx.cookies.get('token');
            } else {
                return null;
            }
        },
        unless: { path: ["/login","/auth"] }
    };


    config.jwtHandler = {
        public: '/login',
    };

    config.authz = {
        enable: true,
        ignore(ctx) {
            const reg = /\/login|\/public|\/api\/v1\/colors/g;
            // console.log(ctx.request.url);
            // console.log(reg.test(ctx.request.url));
            if(ctx.request.url.indexOf('/login')){
                return true
            }
            if(ctx.request.url.indexOf('/auth')){
                return true
            }
            return reg.test(ctx.request.url);
        }
    };
  config.redis = {
    client: {
      host: process.env.EGG_REDIS_HOST || '127.0.0.1',
      port: process.env.EGG_REDIS_PORT || 6379,
      password: process.env.EGG_REDIS_PASSWORD || '',
      db: process.env.EGG_REDIS_DB || '0',
    },
  };

  config.qywechat = {
    corpid: "wx77ed855233bbab23",
    corpsecret: "rtvCBzRBMECqfZpYz5Wfa7GZ4oNDNvr8RnkUam1nqsg"
  };
  config.security = {
    csrf: false,
  };

  config.mongoose = {
    // url: `mongodb://127.0.0.1:27017/${DB}`,
      url: 'mongodb://admin:exe123@39.105.62.145:3001/FileDisk?authSource=FileDisk',
    options: {},
  };

  
  config.cors = {
    origin:'*',
    allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH'
  };
  
  // add your user config here
  const userConfig = {
    // myAppName: 'egg',
  };


  config.exeQiniu = {
    ak: 'hz0JKqSMOkH5H70j6NAdn4HUjLwm0qAC53N69-Ew', // Access Key
    sk: '-_nJNtKqIdTdRTuVYdXktgu3kKYN8iV4LoKwyMgF', // Secret Key
    bucket: 'yfs-web',
    baseUrl: 'http://netdisk.moyufed.com', // 用于拼接已上传文件的完整地址
    app: true,
    agent: false,
  };


  return {
    ...config,
    ...userConfig
  };
};
